using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Media_Renamer
{
    partial class CustomMessageBoxForm : Form
    {
        private string m_Caption = "";
        private string m_Message = "";
        private CustomMessageBoxButtons m_Buttons = CustomMessageBoxButtons.OKOnly;
        private MessageBoxIcon m_MessageIcon = MessageBoxIcon.None;
        private CustomMessageBoxDefaultButton m_DefaultButton = CustomMessageBoxDefaultButton.OK | CustomMessageBoxDefaultButton.Yes;
        private MessageBoxOptions m_Options;
        private CustomDialogResult m_Result;

        public CustomMessageBoxForm()
        {
            InitializeComponent();
        }

        public string Caption
        {
            get { return m_Caption; }
            set { m_Caption = value;}
        }

        public CustomMessageBoxButtons Buttons
        {
            get { return m_Buttons; }
            set { m_Buttons = value; SetButtonsVisible(m_Buttons); }
        }

        public MessageBoxIcon MessageIcon
        {
            get { return m_MessageIcon; }
            set { m_MessageIcon = value; pictureBox1.Visible = (m_MessageIcon != MessageBoxIcon.None); }
        }

        public CustomMessageBoxDefaultButton DefaultButton
        {
            get { return m_DefaultButton; }
            set { m_DefaultButton = value; }
        }

        public string Message
        {
            get { return m_Message; }
            set { m_Message = value; lblMessage.Text = m_Message; }
        }

        public MessageBoxOptions Options
        {
            get { return m_Options; }
            set { m_Options = value; }
        }

        public CustomDialogResult Result
        {
            get { return m_Result; }
            set { m_Result = value; }
        }

        private void SetButtonsInvisible()
        {
            btnAbort.Visible = false;
            btnCancel.Visible = false;
            btnNo.Visible = false;
            btnNoToAll.Visible = false;
            btnOk.Visible = false;
            btnRetry.Visible = false;
            btnYes.Visible = false;
            btnYesToAll.Visible = false;
            btnIgnore.Visible = false;
        }

        private void SetButtonsVisible(CustomMessageBoxButtons buttons) {
            SetButtonsInvisible();
            switch (buttons)
            {
                case CustomMessageBoxButtons.OKOnly:
                    btnOk.Visible = true;
                    break;
                case CustomMessageBoxButtons.OKCancel:
                    btnOk.Visible = true;
                    btnOk.SendToBack();
                    btnCancel.Visible = true;
                    btnCancel.SendToBack();
                    break;
                case CustomMessageBoxButtons.AbortRetryIgnore:
                    btnAbort.Visible = true;
                    btnAbort.SendToBack();
                    btnRetry.Visible = true;
                    btnRetry.SendToBack();
                    btnIgnore.Visible = true;
                    btnIgnore.SendToBack();
                    break;
                case CustomMessageBoxButtons.RetryCancel:
                    btnRetry.Visible = true;
                    btnRetry.SendToBack();
                    btnCancel.Visible = true;
                    btnCancel.SendToBack();
                    break;
                case CustomMessageBoxButtons.YesNo:
                    btnYes.Visible = true;
                    btnYes.SendToBack();
                    btnNo.Visible = true;
                    btnNo.SendToBack();
                    break;
                case CustomMessageBoxButtons.YesYesToAllNoNoToAll:
                    btnYes.Visible = true;
                    btnYes.SendToBack();
                    btnYesToAll.Visible = true;
                    btnYesToAll.SendToBack();
                    btnNo.Visible = true;
                    btnNo.SendToBack();
                    btnNoToAll.Visible = true;
                    btnNoToAll.SendToBack();
                    break;
                case CustomMessageBoxButtons.YesNoCancel:
                    btnYes.Visible = true;
                    btnYes.SendToBack();
                    btnNo.Visible = true;
                    btnNo.SendToBack();
                    btnCancel.Visible = true;
                    btnCancel.SendToBack();
                    break;
                case CustomMessageBoxButtons.YesYesToAllNoNoToAllCancel:
                    btnYes.Visible = true;
                    btnYes.SendToBack();
                    btnYesToAll.Visible = true;
                    btnYesToAll.SendToBack();
                    btnNo.Visible = true;
                    btnNo.SendToBack();
                    btnNoToAll.Visible = true;
                    btnNoToAll.SendToBack();
                    btnCancel.Visible = true;
                    btnCancel.SendToBack();
                    break;
                default:
                    break;
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (button == btnAbort) {
                m_Result = CustomDialogResult.Abort;
            }
            else if (button == btnCancel)
            {
                m_Result = CustomDialogResult.Cancel;
            }
            else if (button == btnIgnore)
            {
                m_Result = CustomDialogResult.Ignore;
            }
            else if (button == btnNo)
            {
                m_Result = CustomDialogResult.No;
            }
            else if (button == btnNoToAll)
            {
                m_Result = CustomDialogResult.NoToAll;
            }
            else if (button == btnOk)
            {
                m_Result = CustomDialogResult.OK;
            }
            else if (button == btnRetry)
            {
                m_Result = CustomDialogResult.Retry;
            }
            else if (button == btnYes)
            {
                m_Result = CustomDialogResult.Yes;
            }
            else if (button == btnYesToAll)
            {
                m_Result = CustomDialogResult.YesToAll;
            }
            this.Close();
        }

        private void CustomMessageBoxForm_Load(object sender, EventArgs e)
        {
            this.Text = m_Caption; 
            this.lblMessage.Location = m_MessageIcon == MessageBoxIcon.None ?new Point(13, lblMessage.Location.Y) : new Point(pictureBox1.Location.X + pictureBox1.Width + 6, lblMessage.Location.Y);
            
            Size messageSize = new Size(lblMessage.Size.Width + 13, this.lblMessage.Height + 50 + lblMessage.Location.Y + flowLayoutPanel1.Height);
            int buttonsWidth = 0;
            foreach (Button button in flowLayoutPanel1.Controls)
            {
                if (button.Visible)
                {
                    buttonsWidth += button.Width + 5 * 2;
                }
            }
            this.Size = messageSize.Width < buttonsWidth ? new Size(buttonsWidth, messageSize.Height) : messageSize;
        }
    }

    public class CustomMessageBox
    {
        private static string m_DefaultCaption = "";
        private static CustomMessageBoxButtons m_DefaultCustomMessageBoxButtons = CustomMessageBoxButtons.OKOnly;
        private static MessageBoxIcon m_DefaultMessageBoxIcon = MessageBoxIcon.None;
        private static CustomMessageBoxDefaultButton m_DefaultCustomMessageBoxDefaultButton = CustomMessageBoxDefaultButton.OK | CustomMessageBoxDefaultButton.Yes;
        
        // Summary:
        //     Displays a message box with specified text.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        public static CustomDialogResult Show(string text)
        {
            return CustomMessageBox.Show(text, m_DefaultCaption);
        }
        //
        // Summary:
        //     Displays a message box with specified text and caption.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        //
        //   caption:
        //     The text to display in the title bar of the message box.
        //
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        public static CustomDialogResult Show(string text, string caption)
        {
            return CustomMessageBox.Show(text, caption, m_DefaultCustomMessageBoxButtons);
        }
        //
        // Summary:
        //     Displays a message box with specified text, caption, and buttons.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        //
        //   caption:
        //     The text to display in the title bar of the message box.
        //
        //   buttons:
        //     One of the System.Windows.Forms.MessageBoxButtons values that specifies which
        //     buttons to display in the message box.
        //
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        //
        // Exceptions:
        //   System.ComponentModel.InvalidEnumArgumentException:
        //     The buttons parameter specified is not a member of System.Windows.Forms.MessageBoxButtons.
        //
        //   System.InvalidOperationException:
        //     An attempt was made to display the System.Windows.Forms.MessageBox in a process
        //     that is not running in User Interactive mode. This is specified by the System.Windows.Forms.SystemInformation.UserInteractive
        //     property.
        public static CustomDialogResult Show(string text, string caption, CustomMessageBoxButtons buttons)
        {
            return CustomMessageBox.Show(text, caption, buttons, m_DefaultMessageBoxIcon);
        }
        //
        // Summary:
        //     Displays a message box with specified text, caption, buttons, and icon.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        //
        //   caption:
        //     The text to display in the title bar of the message box.
        //
        //   buttons:
        //     One of the System.Windows.Forms.MessageBoxButtons values that specifies which
        //     buttons to display in the message box.
        //
        //   icon:
        //     One of the System.Windows.Forms.MessageBoxIcon values that specifies which
        //     icon to display in the message box.
        //
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        //
        // Exceptions:
        //   System.ComponentModel.InvalidEnumArgumentException:
        //     The buttons parameter specified is not a member of System.Windows.Forms.MessageBoxButtons.-or-
        //     The icon parameter specified is not a member of System.Windows.Forms.MessageBoxIcon.
        //
        //   System.InvalidOperationException:
        //     An attempt was made to display the System.Windows.Forms.MessageBox in a process
        //     that is not running in User Interactive mode. This is specified by the System.Windows.Forms.SystemInformation.UserInteractive
        //     property.
        public static CustomDialogResult Show(string text, string caption, CustomMessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return CustomMessageBox.Show(text, caption, buttons, icon, m_DefaultCustomMessageBoxDefaultButton);
        }
        //
        // Summary:
        //     Displays a message box with the specified text, caption, buttons, icon, and
        //     default button.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        //
        //   caption:
        //     The text to display in the title bar of the message box.
        //
        //   buttons:
        //     One of the System.Windows.Forms.MessageBoxButtons values that specifies which
        //     buttons to display in the message box.
        //
        //   icon:
        //     One of the System.Windows.Forms.MessageBoxIcon values that specifies which
        //     icon to display in the message box.
        //
        //   default Button:
        //     One of the System.Windows.Forms.MessageBoxDefaultButton values that specifies
        //     the default button for the message box.
        //
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        //
        // Exceptions:
        //   System.ComponentModel.InvalidEnumArgumentException:
        //     buttons is not a member of System.Windows.Forms.MessageBoxButtons.-or- icon
        //     is not a member of System.Windows.Forms.MessageBoxIcon.-or- defaultButton
        //     is not a member of System.Windows.Forms.MessageBoxDefaultButton.
        //
        //   System.InvalidOperationException:
        //     An attempt was made to display the System.Windows.Forms.MessageBox in a process
        //     that is not running in User Interactive mode. This is specified by the System.Windows.Forms.SystemInformation.UserInteractive
        //     property.
        public static CustomDialogResult Show(string text, string caption, CustomMessageBoxButtons buttons, MessageBoxIcon icon, CustomMessageBoxDefaultButton defaultButton)
        {
            return CustomMessageBox.ShowMessage(text, caption, buttons, icon, defaultButton);
        }
        //
        // Summary:
        //     Displays a message box with the specified text, caption, buttons, icon, default
        //     button, and options.
        //
        // Parameters:
        //   text:
        //     The text to display in the message box.
        //
        //   caption:
        //     The text to display in the title bar of the message box.
        //
        //   buttons:
        //     One of the System.Windows.Forms.MessageBoxButtons values that specifies which
        //     buttons to display in the message box.
        //
        //   icon:
        //     One of the System.Windows.Forms.MessageBoxIcon values that specifies which
        //     icon to display in the message box.
        //
        //   defaultButton:
        //     One of the System.Windows.Forms.MessageBoxDefaultButton values that specifies
        //     the default button for the message box.
        //
        //   options:
        //     One of the System.Windows.Forms.MessageBoxOptions values that specifies which
        //     display and association options will be used for the message box. You may
        //     pass in 0 if you wish to use the defaults.
        //
        // Returns:
        //     One of the System.Windows.Forms.DialogResult values.
        //
        // Exceptions:
        //   System.ComponentModel.InvalidEnumArgumentException:
        //     buttons is not a member of System.Windows.Forms.MessageBoxButtons.-or- icon
        //     is not a member of System.Windows.Forms.MessageBoxIcon.-or- The defaultButton
        //     specified is not a member of System.Windows.Forms.MessageBoxDefaultButton.
        //
        //   System.InvalidOperationException:
        //     An attempt was made to display the System.Windows.Forms.MessageBox in a process
        //     that is not running in User Interactive mode. This is specified by the System.Windows.Forms.SystemInformation.UserInteractive
        //     property.
        //
        //   System.ArgumentException:
        //     options specified both System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly
        //     and System.Windows.Forms.MessageBoxOptions.ServiceNotification.-or- buttons
        //     specified an invalid combination of System.Windows.Forms.MessageBoxButtons.
        public static CustomDialogResult Show(string text, string caption, CustomMessageBoxButtons buttons, MessageBoxIcon icon, CustomMessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            return CustomMessageBox.ShowMessage(text, caption, buttons, icon, defaultButton, options);
        }

        private static CustomDialogResult ShowMessage(string text, string caption, CustomMessageBoxButtons buttons, MessageBoxIcon icon, CustomMessageBoxDefaultButton defaultButton)
        {
            CustomMessageBoxForm frmDialog = new CustomMessageBoxForm();
            frmDialog.Buttons = buttons;
            frmDialog.Caption = caption;
            frmDialog.MessageIcon = icon;
            frmDialog.Message = text;
            frmDialog.DefaultButton = defaultButton;
            frmDialog.ShowDialog();
            return frmDialog.Result;
        }

        private static CustomDialogResult ShowMessage(string text, string caption, CustomMessageBoxButtons buttons, MessageBoxIcon icon, CustomMessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            CustomMessageBoxForm frmDialog = new CustomMessageBoxForm();
            frmDialog.Buttons = buttons;
            frmDialog.Caption = caption;
            frmDialog.MessageIcon = icon;
            frmDialog.Message = text;
            frmDialog.DefaultButton = defaultButton;
            frmDialog.Options = options;
            frmDialog.ShowDialog();
            return frmDialog.Result;
            
        }
    }
    public enum CustomMessageBoxButtons
    {
        OKOnly,
        OKCancel,
        AbortRetryIgnore,
        RetryCancel,
        YesNo,
        YesYesToAllNoNoToAll,
        YesNoCancel,
        YesYesToAllNoNoToAllCancel,
        
    }

    public enum CustomMessageBoxDefaultButton
    {
        OK,
        Cancel,
        Abort,
        Retry,
        Ignore,
        Yes,
        No,
        YesToAll,
        NoToAll,

    }

    public enum CustomDialogResult
    {
        OK,
        Cancel,
        Abort,
        Retry,
        Ignore,
        Yes,
        No,
        YesToAll,
        NoToAll,
    }
}